#pragma once
/**
 * 封装一个连接
 */


#include <functional>
#include <sys/epoll.h>

class EventLoop; // 前向声明

class Channel {
public:
    typedef std::function<void()> EventCallback;

    Channel(EventLoop* loop, int fd);
    ~Channel();

    void handleEvent();

    void setReadCallback(const EventCallback& cb);
    void setWriteCallback(const EventCallback& cb);
    void setCloseCallback(const EventCallback& cb);
    void setErrorCallback(const EventCallback& cb);
    void setAnythingCallback(const EventCallback& cb);  //留下扩展 方便定时器任务等

    void enableReading();
    void enableWriting();
    void disableWriting();
    void disableAll();
    bool isWriting() const;

    int getFd() const;
    int getEvents() const;
    void setRevents(int revt);

    int index() const;
    void setIndex(int idx);

    EventLoop* ownerLoop();

private:
    void update();

    EventLoop* loop_;
    const int fd_;
    int events_;
    int revents_;
    int index_; // poller中使用

    EventCallback readCallback_;
    EventCallback writeCallback_;
    EventCallback closeCallback_;
    EventCallback errorCallback_;
    EventCallback anythingCallback_;
};